package o2o.task.demo;

import com.alibaba.fastjson.JSONObject;
import o2o.cache.company.postman.PostCompanyMemory;
import o2o.config.dao.UserDaoSql;
import o2o.dao.PO.PojoTables;
import o2o.dao.PO.company.postman.PostCompanyInfo;
import o2o.dao.PO.order.ORDER_GROUP_STATE;
import o2o.dao.PO.order.ORDER_STATUS;
import o2o.dao.TO.sync.CloudOrderShopTO_Yiyi;
import o2o.dao.TO.sync.CloudOrderTo_Yiyi;
import o2o.dao.TO.sync.OrderGoodsTO;
import o2o.dao.TO.sync.OrderTO;
import o2o.service.sync.DataSyncService;
import o2o.system.data.SYS_SETTINGS;
import o2o.utils.OrderUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Component
public class MyTaskAnnotation {


    private static String url = "http://47.104.221.189/pics/o2o_jingshan/goods/20181027OJA2KFTAjgibCf4Vdz.png";
//    /**
//     * 定时计算。每天凌晨 01:00 执行一次
//     */
//    @Scheduled(cron = "0 0 1 * * *")
//    public void show(){
//        System.out.println("Annotation：is show run");
//    }
//

    /**
     * 心跳更新。启动时执行一次，之后每隔2秒执行一次
     */
    @Scheduled(cron = "0 */1 * * * ?") // 每隔1MIN执行一次
    public void print() {
        if (SYS_SETTINGS.SYSTEM_TEST == null || SYS_SETTINGS.SYSTEM_TEST == 0) {
            return;
        }
        StringBuilder sql = new StringBuilder();
        sql.append("select count(1) as counts from ")
                .append(PojoTables.OrderInfo)
                .append(" where delFlag = 0 and orderStatus =").append(ORDER_STATUS.SHOP_SURE)
                .append(" and groupState = 0");
        long count = 0;
        try {
            count = UserDaoSql.getInstance().getCount(sql.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (count > 5) {
            return;
        }
        for (int index = 0; index < 5; index++) {

            OrderTO orderTo = new OrderTO();
            orderTo.setOrderNo(OrderUtils.getOrderNo()).setTotalCount(5)
                    .setTotalPrice(51.05D).setDeliveryPrice(1.0D)
                    .setTypeId(1L)
                    .setPreDayNo((int) (Math.random()) * 20).setCanGrabCompanyIds(createCompanyId())
                    .setShopId(1L)
                    .setShopName(createName()).setShopPhone("15269020596")
                    .setShopImageUrl(url).setShopAddress("力高国际").setShopLat(117.09744).setShopLng(36.787371)
                    .setBuyer(createName())
                    .setTelephone("18854000080").setAddress("小于姐姐家").setAddress2("三号楼").setUsername("18854000080")
                    .setLng(117.08744).setLat(36.687371).setProvince("山东").setCity("济南").setShopCity("济南").setCounty("历下区");
            List<OrderGoodsTO> list = new ArrayList<>();
            for (int l = 0; l < 3; l++) {
                list.add(createOrderGoods());
            }
            orderTo.setGoods(list);
            String json = JSONObject.toJSONString(orderTo);
            boolean b = DataSyncService.getInstance().syncOrder(json);
            System.out.println("生成订单----》" + b);
        }
    }

    public static String createCompanyId() {
        List<PostCompanyInfo> list = PostCompanyMemory.getInstance().listOfCity("济南", null);
        Set<Long> set = new HashSet<>();
        int len = ((int) (Math.random() * 1000) % list.size()) + 1;

        for (int index = 0; index < len; index++) {
            set.add(list.get((int) (Math.random() * 100) % list.size()).getId());
        }
        return StringUtils.join(set, ",");
    }


    public static OrderGoodsTO createOrderGoods() {

        OrderGoodsTO orderGoodsTO = new OrderGoodsTO();
        return orderGoodsTO.setId(null).setShopId(1L)
                .setGoodsId(1L).setName(createName())
                .setImageUrl(url).setPrice(1.0D).setTotalCount(1.0)
                .setTotalPrice(0.8D).setPacketPrice(0.2D)
                .setTotalMoney(1.0d).setTotalPacketPrice(0.2);
    }

    public static String createName() {
        String name[] = {"杨", "董", "永", "帅", "同", "宽", "帅", "暴", "了"};
        String restult = "";
        for (int index = 0; index < 3; index++) {
            int a = (int) (Math.random() * 1000) % name.length;
            restult += a;
        }
        return restult;
    }


    // 生产聚合订单
    @Scheduled(cron = "0 */1 * * * ?") // 每隔1MIN执行一次
    public void print2() {
        if (SYS_SETTINGS.SYSTEM_TEST == null || SYS_SETTINGS.SYSTEM_TEST == 0) {
            return;
        }
        StringBuilder sql = new StringBuilder();
        sql.append("select count(1) as counts from ")
                .append(PojoTables.OrderInfo)
                .append(" where delFlag = 0 and orderStatus =").append(ORDER_STATUS.SHOP_SURE)
                .append(" and groupState = 1");
        long count = 0;
        try {
            count = UserDaoSql.getInstance().getCount(sql.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (count > 5) {
            return;
        }
        // 小于10 再生产10条
        for (int index = 0; index < 5; index++) {
            CloudOrderTo_Yiyi orderTo_yiyi = new CloudOrderTo_Yiyi();
            orderTo_yiyi.setOrderNo(OrderUtils.getOrderNo())
                    .setTotalCount(6).setTotalPrice(8.00D)
                    .setDeliveryPrice(2.00d).setTypeId(1L).setCanGrabCompanyIds(createCompanyId())
                    .setPlateAllowance(2.0d).setBuyer(createName())
                    .setTelephone("18854000080").setAddress("小于姐姐家").setAddress2("三号楼").setUsername("18854000080")
                    .setLng(117.12744).setLat(36.687371).setProvince("山东").setCity("济南").setShopCity("济南").setCounty("历下区")
                    .setGroupState(ORDER_GROUP_STATE.GROUP_STATE_PARENT);

            orderTo_yiyi.setShop(createCloudShop());
            String json = JSONObject.toJSONString(orderTo_yiyi);
            boolean b = DataSyncService.getInstance().syncCloudOrder(json);
            System.out.println("生成聚合订单订单----》" + b);
        }
    }

    // 创建店铺的订单
    public List<CloudOrderShopTO_Yiyi> createCloudShop() {
        List<CloudOrderShopTO_Yiyi> list = new ArrayList<>();
        for (int index = 0; index < 2; index++) {
            CloudOrderShopTO_Yiyi cloudOrderShopTO_yiyi = new CloudOrderShopTO_Yiyi();
            cloudOrderShopTO_yiyi.setShopId(Long.parseLong(index + ""))
                    .setShopImageUrl(url).setShopAddress("花园国际").setShopLat(36.677371).setShopLng(117.07744).setShopName(createName()).setShopPhone("15269020596");
            List<OrderGoodsTO> tempList = new ArrayList<>();
            for (int i = 0; i < 2; i++) {
                tempList.add(createOrderGoods());
            }
            cloudOrderShopTO_yiyi.setGoods(tempList);
            list.add(cloudOrderShopTO_yiyi);
        }
        return list;
    }


}
